package com.aruba.canvasapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;


/**
 * 演示Region的View
 */
public class RegionView extends View {
    private Paint mPaint = new Paint();
    
    public RegionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect rect =  new Rect(50, 50, 550, 550);
        Region region = new Region(rect);
        Path path = new Path();
        path.addRoundRect(new RectF(rect),300,300, Path.Direction.CCW);
        //将圆形和正方形区域结合
        region.setPath(path,region);

        //结合区域迭代器使用（得到图形里面的所有的矩形区域）
        RegionIterator iterator = new RegionIterator(region);

        Rect rect1 = new Rect();
        while (iterator.next(rect1)) {
            canvas.drawRect(rect1, mPaint);
        }
    }
}
